package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAffiliatedUnits;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAffiliatedUnitsRel;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAffiliatedUnitsRelService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAffiliatedUnitsService;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Description：挂靠及合作商信息MQ消息接收处理类</p>
 *
 * @author panhj
 * @version 1.0
 * @createDate 2020-07-01
 */
@AllArgsConstructor
@Slf4j
@Component
public class AffiliationInfoProcessor implements AfsMqBizProcessor<AffiliationRelInfo> {

    private final ChannelAffiliatedUnitsService channelAffiliatedUnitsService;
    private final ChannelAffiliatedUnitsRelService channelAffiliatedUnitsRelService;

    @Override
    public boolean processMessage(AffiliationRelInfo entity) throws Exception {
        this.AffiliationStatusQueue(entity);
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_SAVE_AFFILIATION;
    }

    @Transactional(rollbackFor = Exception.class)
    public String AffiliationStatusQueue(AffiliationRelInfo affiliationRelInfo) {
        String errorMsg = "";
        try {
            log.info("开始同步挂靠合作商信息信息……", affiliationRelInfo);
            ChannelAffiliatedUnits channelAffiliatedUnits = this.channelAffiliatedUnitsArrayList(affiliationRelInfo);
            ChannelAffiliatedUnits channelAffiliatedUnits1 = channelAffiliatedUnitsService.getOne(Wrappers.<ChannelAffiliatedUnits>query().lambda()
                    .eq(ChannelAffiliatedUnits::getBusiType, channelAffiliatedUnits.getBusiType())
                    .eq(ChannelAffiliatedUnits::getSocUniCrtCode, channelAffiliatedUnits.getSocUniCrtCode()));
            if (channelAffiliatedUnits1 != null) {
                channelAffiliatedUnitsService.removeById(channelAffiliatedUnits1);
                List<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsRelList = channelAffiliatedUnitsRelService.list(Wrappers.<ChannelAffiliatedUnitsRel>query().lambda()
                        .eq(ChannelAffiliatedUnitsRel::getArriliatedId, channelAffiliatedUnits1.getId()));
                if (channelAffiliatedUnitsRelList != null) {
                    for (int i = 0; i < channelAffiliatedUnitsRelList.size(); i++) {
                        channelAffiliatedUnitsRelService.removeById(channelAffiliatedUnitsRelList.get(i));
                    }
                }
            }
            if (channelAffiliatedUnits != null) {
                channelAffiliatedUnitsService.saveOrUpdate(channelAffiliatedUnits);
            }
            ArrayList<ChannelAffiliatedUnitsRel> affiliatedUnitsRelArrayList = this.channelAffiliatedUnitsRelArrayList(affiliationRelInfo);
            if (affiliatedUnitsRelArrayList != null && affiliatedUnitsRelArrayList.size() > 0) {
                channelAffiliatedUnitsRelService.saveOrUpdateBatch(affiliatedUnitsRelArrayList, affiliatedUnitsRelArrayList.size());
            }
            log.info("########开始同步挂靠合作商信息########", affiliationRelInfo);

        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error("同步挂靠合作商信息失败", affiliationRelInfo, e);
        }
        return errorMsg;
    }

    /**
     * 解析挂靠信息
     */
    private ChannelAffiliatedUnits channelAffiliatedUnitsArrayList(AffiliationRelInfo affiliationRelInfo) {
        ChannelAffiliatedUnits channelAffiliatedUnits = channelAffiliatedUnitsService.getOne(Wrappers.<ChannelAffiliatedUnits>query().lambda()
                .eq(ChannelAffiliatedUnits::getId, affiliationRelInfo.getAffiliationDto().getId()));
        if (channelAffiliatedUnits == null) {
            channelAffiliatedUnits = new ChannelAffiliatedUnits();
        }
        BeanUtil.copyProperties(affiliationRelInfo.getAffiliationDto(), channelAffiliatedUnits, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return channelAffiliatedUnits;
    }

    /**
     * 解析合作商信息
     */
    private ArrayList<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsRelArrayList(AffiliationRelInfo affiliationRelInfo) {
        ArrayList<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsRelArrayList = new ArrayList<>();
        if (affiliationRelInfo.getAffiliationRelDtoArrayList() != null) {
            affiliationRelInfo.getAffiliationRelDtoArrayList().forEach(AffiliationRelDto -> {
                ChannelAffiliatedUnitsRel channelAffiliatedUnitsRel = channelAffiliatedUnitsRelService.getOne(Wrappers.<ChannelAffiliatedUnitsRel>query().lambda()
                        .eq(ChannelAffiliatedUnitsRel::getId, AffiliationRelDto.getId()));
                if (channelAffiliatedUnitsRel == null) {
                    channelAffiliatedUnitsRel = new ChannelAffiliatedUnitsRel();
                }
                BeanUtil.copyProperties(AffiliationRelDto, channelAffiliatedUnitsRel, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                channelAffiliatedUnitsRelArrayList.add(channelAffiliatedUnitsRel);
            });
        }
        return channelAffiliatedUnitsRelArrayList;
    }

}
